package com.quick.system.service.impl;

import java.util.Date;
import java.util.List;

import com.quick.common.core.domain.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import com.quick.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.quick.system.domain.DutyNotice;
import com.quick.system.mapper.DutyScheduleMapper;
import com.quick.system.domain.DutySchedule;
import com.quick.system.service.IDutyScheduleService;

/**
 * 值班排班Service业务层处理
 *
 * @author quick
 * @date 2025-01-03
 */
@Service
public class DutyScheduleServiceImpl implements IDutyScheduleService {
    @Autowired
    private DutyScheduleMapper dutyScheduleMapper;
    @Autowired
    private SysUserServiceImpl sysUserServiceImpl;

    /**
     * 查询值班排班
     *
     * @param id 值班排班主键
     * @return 值班排班
     */
    @Override
    public DutySchedule selectDutyScheduleById(Long id) {
        return dutyScheduleMapper.selectDutyScheduleById(id);
    }

    /**
     * 查询值班排班列表
     *
     * @param dutySchedule 值班排班
     * @return 值班排班
     */
    @Override
    public List<DutySchedule> selectDutyScheduleList(DutySchedule dutySchedule) {
        return dutyScheduleMapper.selectDutyScheduleList(dutySchedule);
    }

    /**
     * 新增值班排班
     *
     * @param dutySchedule 值班排班
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDutySchedule(DutySchedule dutySchedule) {
        int rows = dutyScheduleMapper.insertDutySchedule(dutySchedule);
        insertDutyNotice(dutySchedule);
        return rows;
    }

    /**
     * 修改值班排班
     *
     * @param dutySchedule 值班排班
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDutySchedule(DutySchedule dutySchedule) {
        dutyScheduleMapper.deleteDutyNoticeByScheduleId(dutySchedule.getId());
        insertDutyNotice(dutySchedule);
        return dutyScheduleMapper.updateDutySchedule(dutySchedule);
    }

    /**
     * 批量删除值班排班
     *
     * @param ids 需要删除的值班排班主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDutyScheduleByIds(Long[] ids) {
        dutyScheduleMapper.deleteDutyNoticeByScheduleIds(ids);
        return dutyScheduleMapper.deleteDutyScheduleByIds(ids);
    }

    /**
     * 删除值班排班信息
     *
     * @param id 值班排班主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDutyScheduleById(Long id) {
        dutyScheduleMapper.deleteDutyNoticeByScheduleId(id);
        return dutyScheduleMapper.deleteDutyScheduleById(id);
    }

    /**
     * 新增值班通知信息
     *
     * @param dutySchedule 值班排班对象
     */
    public void insertDutyNotice(DutySchedule dutySchedule) {
        List<DutyNotice> dutyNoticeList = dutySchedule.getDutyNoticeList();
        Long id = dutySchedule.getId();
        if (StringUtils.isNotNull(dutyNoticeList)) {
            List<DutyNotice> list = new ArrayList<DutyNotice>();
            Map<String, List<SysUser>> collect = dutyNoticeList.stream()
                    .map(DutyNotice::getUserId)
                    .map(userId -> sysUserServiceImpl.selectUserById(userId))
                    .collect(Collectors.groupingBy(SysUser::getSex));
            if (collect.size() != 1) {
                throw new RuntimeException("值班通知人员不能为异性组合");
            }

            for (DutyNotice dutyNotice : dutyNoticeList) {
                dutyNotice.setScheduleId(id);
                dutyNotice.setNoticeSendTime(new Date());
                dutyNotice.setStatus(0);
                list.add(dutyNotice);
            }
            if (!list.isEmpty()) {
                dutyScheduleMapper.batchDutyNotice(list);
            }
        }
    }
}
